package com.ray.auth.system.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.auth.system.emun.OpenType;
import com.ray.auth.system.emun.UseStatus;
import com.ray.auth.system.service.NoticeService;
import com.ray.auth.system.table.entity.Notice;
import com.ray.auth.system.table.entity.NoticeExample;
import com.ray.auth.system.table.mapper.NoticeMapper;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;
@Service
public class NoticeServiceImpl extends BaseService implements NoticeService{
	/**日志对象**/
	private static Logger logger = Logger.getLogger(NoticeServiceImpl.class);
	
	@Autowired
	private NoticeMapper noticeMapper;

	@Transactional
	public Result<Object> addNotice(Notice params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		params.setTenantId(user.getTenantId());
		params.setNoticeId(UUIDUtil.getUUID());
		params.setIsDelete(DeleteEnum.USE.getValue());
		this.setUserForAdd(params, user);
		int count=noticeMapper.insertSelective(params);
		if(count!=1){
			logger.error("NoticeServiceImpl=>addNotice is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
	
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Notice queryNotice(String id, LoginUser user) {
		return noticeMapper.selectByPrimaryKey(id);
		}
	
	
    @Transactional
	public Result<Object> updateNotice(Notice params, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		this.setUserForUpdate(params, user);
		int count=noticeMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("NoticeServiceImpl=>editNotice is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional
	public Result<Object> deleteNotice(String id, LoginUser user) {
		Result<Object> result=null;
		result=new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		Notice params=new Notice();
		params.setNoticeId(id);
		params.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(params, user);
		int count=noticeMapper.updateByPrimaryKeySelective(params);
		if(count!=1){
			logger.error("NoticeServiceImpl=>deleteNotice is error");
			throw new BussinessException(-1, ErrorMsg.INSERT_ERROR);
		}
		return result;
	}
    
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<Notice> queryNotice(Notice params, PageDto<Notice> page, LoginUser user) {
		NoticeExample example=new NoticeExample();
		example.createCriteria().andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		this.setExamplePage(example, page);
		page.setTotal(noticeMapper.countByExample(example));
		page.setRecords(noticeMapper.selectByExample(example));
		return page;
	}


    @Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Notice queryFirstWindowNotice(LoginUser user) {
		NoticeExample example=new NoticeExample();
		example.createCriteria().andIsDeleteEqualTo(DeleteEnum.USE.getValue())
		.andNoticeStatusEqualTo(UseStatus.ON.getType())
		.andEndTimeGreaterThanOrEqualTo(new Date()).andOpenTypeEqualTo(OpenType.OUTWINDOW.getType());
		this.setExamplePage(example, new PageDto<Notice>(1, 1, "create_Time desc"));
		List<Notice> notices = noticeMapper.selectByExample(example);
		if(!notices.isEmpty()) return notices.get(0);
		return null;
	}

}
